/*
    11.Write a C program to sort a string array in ascending order. 

    Test Data :
    Input the string : w3resource

    Expected Output :

    After sorting the string appears like : 
    3ceeorrsuw 
*/

#include <stdio.h>
#include <stdlib.h>

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p = ch;
        p++;
    }
    *p = '\0';
    return str;
}

int getstrlen(char *str)
{
    int len = 0;
    char *p = str;
    while(*p != '\0'){
        len++;
        p++;
    }
    len++; // '\0'
    return len;
}

void sort(char *str)
{
    int i,j;
    int len = getstrlen(str) - 1;
    char tmp;
    for(i = 0; i < len - 1; i++){
        for(j = 0; j < len - i - 1; j++){
            if(str[j] > str[j + 1]){
                tmp = str[j + 1];
                str[j + 1] = str[j];
                str[j] = tmp;
            }
        }
    }
}

void maxsort(char *str, int len)
{
    int i;
    char tmp;
    if(len >= 2){
         for(i = 0; i < len - 1; i++){
            if(str[i] > str[i + 1]){
                tmp = str[i + 1];
                str[i + 1] = str[i];
                str[i] = tmp;
            }    
        }
    }else{
        return;
    }  
    maxsort(str, len - 1);       
}

int main(void)
{
    char *str = malloc(1024);
    printf("Input the string : ");
    getstr(str);
    int strlen = getstrlen(str) - 1;
    //sort(str);
    maxsort(str, strlen);
    printf("After sorting the string appears like :\n%s", str);
    return 0;
}